/*****************************************************************************
 * @(#)DataCacheListener.java 1.0.0 2011-4-27 下午05:34:16
 * 
 * Copyright 2011 Thunisoft Co. Ltd.  All rights reserved.
 * THUNISOFT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 ****************************************************************************/
package com.gmail.huxiaowi.cblog.web.listener;

import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.gmail.huxiaowi.cblog.consts.CBlogConsts;
import com.gmail.huxiaowi.cblog.entity.Category;
import com.gmail.huxiaowi.cblog.service.IUserService;
import com.gmail.huxiaowi.cblog.util.JCacheUtils;

/**
 * Class DataCacheListener
 * 
 * @author  huxw
 * @version $Revision:1.0.0, $Date:2011-4-27 下午05:34:16$
 */
public class DataCacheListener implements ServletContextListener {

	private static final Log logger = LogFactory.getLog(DataCacheListener.class);
	
    /* (non-Javadoc)
     * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent arg0) {
        JCacheUtils.remove(CBlogConsts.CACHED_KEY_CATALOGS);
        
        if (logger.isDebugEnabled()) {
        	logger.debug("context destroyed...");
        }
    }

    /* (non-Javadoc)
     * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent sce) {
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
        IUserService userService = (IUserService) ctx.getBean("userBiz");
        
        List<Category> categorys = userService.queryAllCategorys();
        JCacheUtils.put(CBlogConsts.CACHED_KEY_CATALOGS, categorys);
    }

}
